package com.ruoyi.project.beautyflower;

import ai.advance.cmp.api.client.ApiClient;
import ai.advance.cmp.api.client.bean.ResponseBean;
import ai.advance.cmp.api.client.bean.dto.NotifyRepaymentDetailDTO;
import ai.advance.cmp.api.client.bean.dto.query.RepaymentScheduleQueryDTO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.SimpleFormatter;

public class ThreeElementsUtil {
    private static final Logger log = LoggerFactory.getLogger(ThreeElementsUtil.class);
    private static ApiClient apiClient=new ApiClient("http://47.95.71.79:9804", "NDMICRTMD01", "SceeAdxpJzqnGlnz37PmRKUEz7wnPFeR");


    /**
     * 三要素验证
     * @param map
     * @return
     */
    public static ResponseBean threeElementsVerification(Map<String,Object> map) {
        return apiClient.request("/data-gateway/api/v1/service","?cacheType=Y&serviceCode=GEO_S107&partnerId=BLKHLTMD", map);
    }

    public static void main(String[] args) {
//        Map<String,Object> map = Maps.newHashMap();
//        map.put("name","刘兴松");
//        map.put("phoneNumber","15023286425");
//        map.put("idCardNumber","500381199412053517");
//        ResponseBean responseBean=ThreeElementsUtil.threeElementsVerification(map);
//        System.out.println(responseBean);

//        System.out.println(OrderCoderUtil.toCode(Long.parseLong("1")));
//        System.out.println("JM-" + GenerateNum.getInstance().GenerateOrder());
//
//        String str="[\n" +
//                "  {\n" +
//                "    \"contactName\" : \"里\",\n" +
//                "    \"contactMobileNumber\" : \"52259663555\"\n" +
//                "  },\n" +
//                "  {\n" +
//                "    \"contactName\" : \"李\",\n" +
//                "    \"contactMobileNumber\" : \"55563666536\"\n" +
//                "  }\n" +
//                "]";
//        JSONArray jsonArray = JSON.parseArray(str);
//        //遍历方式2
//        for (Object obj : jsonArray) {
//            JSONObject jsonObject = (JSONObject) obj;
//            System.out.println(jsonObject.getString("contactName")+":"+jsonObject.getString("contactMobileNumber"));
//        }

//        RepaymentScheduleQueryDTO repaymentScheduleQueryDTO =new RepaymentScheduleQueryDTO();
//        repaymentScheduleQueryDTO.setUserId("35");
//        repaymentScheduleQueryDTO.setOrderId("TMH201810251124275670001");
//        System.out.println(BfUtil.queryRepaymentScheduleResult(repaymentScheduleQueryDTO));
        String details="[[{\n" +
                "    \"duePrincipalAmount\": \"34.68\",\n" +
                "    \"installmentNo\": \"2\",\n" +
                "    \"orderId\": \"JM-201810171722328970001\",\n" +
                "    \"repaymentInterestAmount\": \"1.35\",\n" +
                "    \"repaymentPrincipalAmount\": \"33.33\"\n" +
                "  },\n" +
                "  {\n" +
                "    \"duePrincipalAmount\": \"34.68\",\n" +
                "    \"installmentNo\": \"2\",\n" +
                "    \"orderId\": \"JM-201810171722328970001\",\n" +
                "    \"repaymentInterestAmount\": \"1.35\",\n" +
                "    \"repaymentPrincipalAmount\": \"33.33\"\n" +
                "  }],\n" +
                "  [{\n" +
                "    \"duePrincipalAmount\": \"34.68\",\n" +
                "    \"installmentNo\": \"2\",\n" +
                "    \"orderId\": \"JM-201810171722328970001\",\n" +
                "    \"repaymentInterestAmount\": \"1.35\",\n" +
                "    \"repaymentPrincipalAmount\": \"33.33\"\n" +
                "  },\n" +
                "  {\n" +
                "    \"duePrincipalAmount\": \"34.68\",\n" +
                "    \"installmentNo\": \"2\",\n" +
                "    \"orderId\": \"JM-201810171722328970001\",\n" +
                "    \"repaymentInterestAmount\": \"1.35\",\n" +
                "    \"repaymentPrincipalAmount\": \"33.33\"\n" +
                "  }]]";
        JSONArray jsonArray = JSON.parseArray(details);
        System.out.println(jsonArray);

        List<NotifyRepaymentDetailDTO> notifyRepaymentDetailDTOList = new ArrayList();//提前还款详细
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONArray jsonArray1=jsonArray.getJSONArray(i);
            for (int i1 = 0; i1 < jsonArray1.size(); i1++) {
                JSONObject jsonObject = (JSONObject) jsonArray1.get(i1);
                NotifyRepaymentDetailDTO notifyRepaymentDetailDTO = new NotifyRepaymentDetailDTO();
                notifyRepaymentDetailDTO.setOrderId(jsonObject.getString("orderId")+"");//订单id
                notifyRepaymentDetailDTO.setInstallmentNo(Integer.parseInt(jsonObject.getString("installmentNo")+""));//还款分期号
                notifyRepaymentDetailDTO.setDuePrincipalAmount(jsonObject.getBigDecimal("duePrincipalAmount"));//应还款本金
                notifyRepaymentDetailDTO.setDueInterestAmount(jsonObject.getBigDecimal("repaymentInterestAmount"));//应还款利息
                notifyRepaymentDetailDTO.setDefaultedInterestAmount(BigDecimal.valueOf(0D));//应还罚息
                notifyRepaymentDetailDTO.setRepaymentPrincipalAmount(jsonObject.getBigDecimal("duePrincipalAmount"));//实际还款本金
                BigDecimal repaymentInterestAmount = jsonObject.getBigDecimal("repaymentInterestAmount");//实际还款利息
                String remark = null;
                if(i>2){
                    notifyRepaymentDetailDTO.setRepaymentInterestAmount(BigDecimal.valueOf(0D));//实际还款利息
                    remark = "提前还款减息";
                }else{
                    notifyRepaymentDetailDTO.setRepaymentInterestAmount(repaymentInterestAmount);//实际还款利息
                }
//            if (i % 3 == 0) {
//                repaymentInterestAmount = BigDecimal.valueOf(0L);
//                remark = "提前还款减息";
//            }
                notifyRepaymentDetailDTO.setRepaymentDefaultedInterestAmount(BigDecimal.valueOf(0D));//实际还款罚息
                notifyRepaymentDetailDTO.setRemark(remark);//备注，如利息减免等有用的信息

//            repaymentAmount = repaymentAmount.add(notifyRepaymentDetailDTO.getRepaymentPrincipalAmount().add(notifyRepaymentDetailDTO.getRepaymentInterestAmount()).add(notifyRepaymentDetailDTO.getRepaymentDefaultedInterestAmount()));
                notifyRepaymentDetailDTOList.add(notifyRepaymentDetailDTO);
            }
        }

//        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        try {
//            long lt = new Long("1539849233740");
//            Date d=new Date(lt);
//            System.out.println(d);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }


    }
}
